/*
 * @Description:
 * @Author: maxyang
 * @Date: 2022-08-09 11:53:44
 * @LastEditTime: 2022-08-10 09:31:07
 * @LastEditors: liutq
 * @Reference:
 */
package aliyun

import (
	"log"

	"github.com/smartwalle/alipay/v3"
	"gopkg.in/ini.v1"
)

var AliClient *alipay.Client

func InitAliClient(conf *ini.File) (*alipay.Client, error) {
	var err error
	if AliClient, err = alipay.New(conf.Section("alipay").Key("appid").String(), conf.Section("alipay").Key("appsecket").String(), false); err != nil {
		log.Println("初始化支付宝失败", err)
		return AliClient, err
	}

	// 使用支付宝证书
	if err = AliClient.LoadAppPublicCertFromFile(conf.Section("alipay").Key("appcertpublickey").String()); err != nil {
		log.Println("加载证书发生错误", err)
		return AliClient, err
	}

	if err = AliClient.LoadAliPayRootCertFromFile(conf.Section("alipay").Key("alipayrootcert").String()); err != nil {
		log.Println("加载证书发生错误", err)
		return AliClient, err
	}
	if err = AliClient.LoadAliPayPublicCertFromFile(conf.Section("alipay").Key("alipaycertpublickey_rsa2").String()); err != nil {
		log.Println("加载证书发生错误", err)
		return AliClient, err
	}
	return AliClient, nil
}
